package com.gitee.target123.develop.common.alioss;

import com.alibaba.fastjson.JSON;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.auth.sts.AssumeRoleRequest;
import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.aliyuncs.cdn.model.v20180510.PushObjectCacheRequest;
import com.aliyuncs.cdn.model.v20180510.PushObjectCacheResponse;
import com.aliyuncs.http.MethodType;
import com.gitee.target123.develop.common.model.AliyunOssToken;
import com.gitee.target123.develop.common.util.CommonUtil;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;

public class AliOssUtil {

    /**
     * 删除一个Object
     * @param aliOss 操作对象
     * @param objectName bucket下的文件名
     */
    public static void deleteObject(AliOss aliOss, String objectName)
            throws OSSException, ClientException {
        OSSClient ossClient = aliOss.getOssClient();
        AliOssProperties aliOssProperties = aliOss.getAliOssProperties();
        ossClient.deleteObject(aliOssProperties.getBucketName(), objectName);
    }

    /**
     * 上传-文件流
     * @param aliOss 操作对象
     * @param key 唯一值
     * @param contentType content-type
     * @param inputStream 输入流
     * @return 访问地址
     */
    public static String upload(AliOss aliOss,String key,String contentType,InputStream inputStream) throws OSSException, ClientException {
        OSSClient ossClient = aliOss.getOssClient();
        AliOssProperties aliOssProperties = aliOss.getAliOssProperties();
        ObjectMetadata objectMeta = new ObjectMetadata();
        objectMeta.setContentType(contentType);
        ossClient.putObject(aliOssProperties.getBucketName(), key, inputStream, objectMeta);

        String hostName = aliOssProperties.getHostName();
        if (!hostName.endsWith("/")) hostName=hostName+"/";

        return hostName + key;
    }

    /**
     * 上传-文件路径
     * @param aliOss 操作对象
     * @param key 唯一值
     * @param contentType content-type
     * @param filePath 文件路径
     * @return 访问地址
     */
    public static String upload(AliOss aliOss,String key,String contentType,String filePath) throws OSSException, ClientException, FileNotFoundException {
        return upload(aliOss,key,contentType,new FileInputStream(filePath));
    }

    /**
     * oss上传权限token
     * @param aliOss 操作对象
     * @param roleSessionName 支持输入 2 ~ 32 个字符,请输入至少 2 个字符,如果只有 1 个字符,会出现错误
     * @param policy 权限配置
     */
    public static AliyunOssToken getUploadToken(AliOss aliOss,String roleSessionName,String policy) throws com.aliyuncs.exceptions.ClientException {
        IAcsClient acsClient = aliOss.getAcsClient();
        AliOssProperties aliOssProperties = aliOss.getAliOssProperties();
        // 创建一个 AssumeRoleRequest 并设置请求参数
        AssumeRoleRequest request = new AssumeRoleRequest();
        request.setMethod(MethodType.POST);
        request.setRoleArn(aliOssProperties.getStsRoleArn());
        request.setRoleSessionName(roleSessionName);

        request.setPolicy(policy);
        request.setDurationSeconds(aliOssProperties.getStsTokenDurationSeconds());

        // 发起请求，并得到response
        AssumeRoleResponse response = acsClient.getAcsResponse(request);
        AssumeRoleResponse.Credentials credentials = response.getCredentials();

        AliyunOssToken token = new AliyunOssToken();
        token.setEndpoint(aliOssProperties.getEndpoint());
        token.setBucketName(aliOssProperties.getBucketName());
        token.setHostName(aliOssProperties.getHostName());
        token.setFilePath(aliOssProperties.getFilePath());
        token.setAccessKeyId(credentials.getAccessKeyId());
        token.setAccessKeySecret(credentials.getAccessKeySecret());
        token.setSecurityToken(credentials.getSecurityToken());
        token.setExpiration(credentials.getExpiration());
        return token;
    }

    /**
     * cdn预热
     * @param aliOss 操作对象
     * @param urls 链接集合
     */
    public static String preheat(AliOss aliOss, List<String> urls) throws com.aliyuncs.exceptions.ClientException{
        PushObjectCacheRequest request = new PushObjectCacheRequest();
        request.setObjectPath(CommonUtil.listToString(urls,"\\n",String::toString));
        PushObjectCacheResponse response = aliOss.getAcsClient().getAcsResponse(request);
        return JSON.toJSONString(response);
    }

}
